/*
 *所有关于tb_project_baseinfo_imitate类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*tb_project_baseinfo_imitateService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using MYData.Core.BaseProvider;
using MYData.Core.Extensions.AutofacManager;
using MYData.Entity.DomainModels;
using System.Linq;
using MYData.Core.Utilities;
using System.Linq.Expressions;
using MYData.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MYData.System.IRepositories;
using System.Net;
using System.Threading.Tasks;
using MYData.Core.EFDbContext;
using MYData.System.IServices;
using System;

namespace MYData.System.Services
{
    public partial class tb_project_baseinfo_imitateService : Itb_project_baseinfo_imitateService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Itb_project_baseinfo_imitateRepository _repository;//访问数据库
        private readonly VOLContext _db;
        [ActivatorUtilitiesConstructor]
        public tb_project_baseinfo_imitateService(
            Itb_project_baseinfo_imitateRepository dbRepository,
            IHttpContextAccessor httpContextAccessor,
            VOLContext db
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            _db = db;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        WebResponseContent webResponse = new WebResponseContent();

        //立项id下拉显示
        public async Task<WebResponseContent> BaseinfoIdSearch()
        {
            var list = from x in _db.Set<tb_project_baseinfo>()
                       select new
                       {
                           x.projectId,
                       };
            return webResponse.OK("success", list);
        }

        //立项基本信息主键查找
        public async Task<WebResponseContent> BaseinfoSearch(string projectId)
        {
            //var parentId = "0";
            //var tbBaseInfo = (await _repository.FindAsync(x => x.projectId == projectId)).ToList();
            //查找项目详细信息
            var list = from x in _db.Set<tb_project_baseinfo>().ToList()
                       join y in _db.Set<tb_project_sourceinfo>().ToList() on x.projectType equals y.Id
                       join z in _db.Set<tb_project_sourceinfo>().ToList() on x.projectLevel equals z.Id
                       join a in _db.Set<tb_project_billing_allocation>().ToList() on x.projectId equals a.projectId
                       join b in _db.Set<tb_project_billing>().ToList() on a.billingId equals b.billingId
                       where x.projectId == projectId
                       select new
                       {
                           x.projectId,
                           x.projectName,
                           x.specialMoney,//专项经费
                           x.supportMoney,//配套经费
                           x.projectMoney,
                           billingTime=b.billingTime.ToString("yyyy-MM-dd"),
                           b.billingMoney,
                           x.projectUndertakeunit,
                           x.projectDepartresult,
                           x.projectPerson,
                           accounting = List(x.projectId).ToString(),//已到账总经费
                           leaveMoney= x.projectMoney- List(x.projectId)
                       };
            return webResponse.OK("success", list);
        }
        /// <summary>
        /// 计算已到账总经费
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        /// 通过汇总分配支出值来计算
        public decimal List(string projectId)
        {
            //来自给定项目的tb_project_billing_allocation表
            //将变量初始化为0
            decimal accounting = 0;
            //查询所有条目的tb_project_billing_allocation表
            var billinglist = from b in _db.Set<tb_project_billing_allocation>()
                                  //给定的投射体和isDelete设置为0(未删除)
                              where b.isDelete == 0 && b.projectId == projectId
                              select new
                              {
                                  //只选择allocationExpenditure值
                                  b.allocationExpenditure,
                              };
            billinglist.ToList().ForEach(billing =>
            {
                //并将分配支出值添加到变量中
                accounting += billing.allocationExpenditure;
            });
            ////返回最终的会计值
            return accounting/10000;
        }
    }
}
